import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/home/warning/alarm_resp_entity.dart';
import 'package:safetymonitor/generated/assets.dart';
import 'package:safetymonitor/modules/home/new_alarm_warning_page/controller.dart';
import 'package:safetymonitor/router/app_pages.dart';
///已完成
class CompletedController extends GetxController {
  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();
  /// 获取列表数据
  /// 设备的完成未完成       就是dataStatus == 1 和dataType==3
  /// 报警预警的完成未完成    就是dataStatus == 1   dataType==1
  Future<List<AlarmRespEntity>> getData(int pageNum, int pageSize, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<AlarmRespEntity>>(Interface.getAlarmPage, queryParameters: {
      'pageNo': pageNum,
      'pageSize': pageSize,
      'dataStatus': 1, //app这边固定传1
      'dataType': Get.find<NewAlarmWarningPageController>().dataType,   //报警预警的=1  设备=3
      'statusType': 1,// statusType==1 已完成

      'companyId': Get.find<NewAlarmWarningPageController>().company2, //公司单位
      'alarmLevel': Get.find<NewAlarmWarningPageController>().level2, //报警级别
      'systemId': Get.find<NewAlarmWarningPageController>().monitorCode2.contains('-')?Get.find<NewAlarmWarningPageController>().monitorCode2.split('-').first:'', //系统id
      'monitorCode': Get.find<NewAlarmWarningPageController>().monitorCode2.contains('-')?Get.find<NewAlarmWarningPageController>().monitorCode2.split('-')[1]:'', //类别
    }).execute();

    List<AlarmRespEntity>? data;
    res.onSuccess((result) {
      data = result.data;
      update();
    });
    return data ?? [];
  }

  /// 进入详情页面
  void goDetails(AlarmRespEntity entity) {
    Get.toNamed(Routes.completedDetailPage, arguments: entity)?.then((value) {
      if (value != null) {
        globalKey.currentState?.onRefresh();
      }
    });
  }



  /// 处置进度状态（1未处置，2.超时未处置，3.处置中，4.超时处置中，5.及时完成，6.超时完成）
  Widget getStatueView(String status) {
    String name = '';
    Color? bgColor;
    switch (status) {
      case '1':
        name = '未处置';
        break;
      case '2':
        name = '超时未处置';
        bgColor = const Color(0xFFFE9843);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
      case '3':
        name = '处置中';
        bgColor = const Color(0xFFFE9843);
        break;
      case '4':
        name = '超时处置中';
        bgColor = const Color(0xFF63C320);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
      case '5':
        name = '及时完成';
        bgColor = const Color(0xFF63C320);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
      case '6':
        name = '超时完成';
        bgColor = const Color(0xFF63C320);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
    }
    return SizedBox(
      child: Stack(
        alignment: Alignment.centerRight,
        children: [
          SvgPicture.asset(
            Assets.svgBgStatue,
            height: 12.w,
            width: 12.w,
            fit: BoxFit.cover,
            colorFilter: bgColor == null ? null : ColorFilter.mode(bgColor, BlendMode.srcIn),
          ),
          Text(
            name,
            style: TextStyle(
              fontSize: 14.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xFF3F425B),
            ),
          ),
        ],
      ),
    );
  }
}
